
#import <EOAccess1x/EOAccess1x.h>

void fetch(void);
void usage(void);

int main (int argc, const char *argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NS_DURING

     fetch();

   NS_HANDLER

     NSLog(@"%@", [localException reason]);

   NS_ENDHANDLER

   [pool release];
   exit(0);       // insure the process exit status is 0
   return 0;      // ...and make main fit the ANSI spec.
}

void fetch(void)
{
    NSString *modelPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"model"];
    NSString *entityName = [[NSUserDefaults standardUserDefaults] stringForKey:@"entity"];

    EODatabaseDataSource *dataSource;
    NSArray *results;

    if(!modelPath || ![modelPath length] || !entityName || ![entityName length]) {
      usage();
      return;
    }

    dataSource = [[EODatabaseDataSource alloc] initWithModelName:modelPath entityName:entityName databaseName:nil contextName:nil channelName:nil];

    if(!dataSource) {
        NSLog(@"Invalid model or entity");
        return;
    }
    
    results = [dataSource fetchObjects];

    NSLog(@"Result: %@", results);
}

void usage(void)
{
    NSLog(@"Usage: fetch -model <eomodel file> -entity <entity name>");
}
